package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.mapper.TbSpecificationMapper;
import com.pinyougou.mapper.TbSpecificationOptionMapper;
import com.pinyougou.pojo.TbSpecification;
import com.pinyougou.pojo.TbSpecificationExample;
import com.pinyougou.pojo.TbSpecificationOption;
import com.pinyougou.pojo.TbSpecificationOptionExample;
import com.pinyougou.sellergoods.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private TbSpecificationMapper specificationMapper;
    @Autowired
    private TbSpecificationOptionMapper specificationOptionMapper;

    @Override
    public int add(TbSpecification specification) {
        //将规格信息添加到规格表
        int acount = specificationMapper.insertSelective(specification);
        //将规格选项添加到规格选项表
        List<TbSpecificationOption> specificationOptions = specification.getSpecificationOptions();
        //循环增加
        for (TbSpecificationOption specificationOption : specificationOptions) {
            specificationOption.setSpecId(specification.getId());
            //增加入库
            specificationOptionMapper.insertSelective(specificationOption);
        }

        return acount;
    }

    @Override
    public TbSpecification getOneById(Long id) {
        TbSpecification specification = specificationMapper.selectByPrimaryKey(id);
        TbSpecificationOptionExample optionExample = new TbSpecificationOptionExample();
        optionExample.createCriteria().andSpecIdEqualTo(id);
        List<TbSpecificationOption> specificationOptionList = specificationOptionMapper.selectByExample(optionExample);
        specification.setSpecificationOptions(specificationOptionList);
        return specification;
    }

    @Override
    public int update(TbSpecification specification) {
        //修改规格信息
        int acount = specificationMapper.updateByPrimaryKeySelective(specification);
        //删除之前的规格
        TbSpecificationOption specificationOption = new TbSpecificationOption();
        specificationOption.setSpecId(specification.getId());
        TbSpecificationOptionExample specificationOptionExample = new TbSpecificationOptionExample();
        specificationOptionExample.createCriteria().andIdEqualTo(specificationOption.getSpecId());
        specificationOptionMapper.deleteByExample(specificationOptionExample);
        //添加新的规格
        List<TbSpecificationOption> specificationOptions = specification.getSpecificationOptions();
        for (TbSpecificationOption option : specificationOptions) {
            specificationOptionMapper.insertSelective(option);
        }
        return acount;
    }


    /**
     * 根据条件分页查询
     */
    @Override
    public PageInfo<TbSpecification> findAll(TbSpecification specification, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        //分页
        TbSpecificationExample example = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = example.createCriteria();

        if (specification != null) {
            //首字母搜索
            if (specification.getSpecName() != null && specification.getSpecName().length() > 0 && specification.getSpecName().trim().length() > 0) {
                criteria.andSpecNameLike(specification.getSpecName());
                System.out.println(specification.getSpecName());
            }

        }
        List<TbSpecification> tbSpecifications = specificationMapper.selectByExample(example);
        return new PageInfo(tbSpecifications);
    }

}
